/**
 * -----------------------------------
 * Copyright (c) 2021-2023
 * All rights reserved, Designed By www.linfeng.tech
 * 林风社交论坛商业版本请务必保留此注释头信息
 * 商业版授权联系技术客服	 QQ:  3582996245
 * 严禁分享、盗用、转卖源码或非法牟利！
 * 版权所有 ，侵权必究！
 * -----------------------------------
 */
package io.linfeng.modules.app.controller;

import io.linfeng.common.utils.Result;
import io.linfeng.common.vo.MessageNumberResponse;
import io.linfeng.common.utils.AppPageUtils;
import io.linfeng.modules.admin.entity.AppUserEntity;
import io.linfeng.modules.admin.service.MessageService;
import io.linfeng.modules.app.annotation.Login;
import io.linfeng.modules.app.annotation.LoginUser;
import io.linfeng.modules.app.param.MessageReadForm;
import io.linfeng.modules.app.param.UpdateChatStatusForm;
import io.linfeng.modules.app.param.UpdateSystemNoticeStatusForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;


/**
 * 
 *  APP 系统消息模块
 * @author linfeng
 * @email 2445465217@qq.com
 * @date 2022-01-26 13:15:30
 */
@RestController
@RequestMapping("app/message")
@Api(tags = "移动端——系统消息模块")
public class AppMessageController {


    @Autowired
    @Lazy
    private MessageService messageService;

    /**
     * 消息列表分页
     */
    @Login
    @GetMapping("/list")
    @ApiOperation("消息列表分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型",paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "page", value = "分页页码",paramType = "query", dataType = "Integer", required = true)
    })
    public Result<AppPageUtils> list(@RequestParam("type") Integer type,
                                     @RequestParam("page") Integer page,
                                     @ApiIgnore @LoginUser AppUserEntity user){
        AppPageUtils pages=messageService.queryMessageList(type,page,user);

        return new Result<AppPageUtils>().ok(pages);
    }


    /**
     * 消息数量
     */
    @PostMapping("/num")
    @ApiOperation("消息数量")
    public Result<MessageNumberResponse> num(){
        MessageNumberResponse response=messageService.getMessageNumber();

        return new Result<MessageNumberResponse>().ok(response);
    }


    @Login
    @GetMapping("/status")
    @ApiOperation("设置消息已读")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型",paramType = "query", dataType = "Integer", required = true)
    })
    public Result status(@RequestParam("type") Integer type,
                    @ApiIgnore @LoginUser AppUserEntity user){
        messageService.status(type, user.getUid());
        return new Result();
    }



    @Login
    @PostMapping("/readMessage")
    @ApiOperation("设置用户点击的消息为已读")
    public Result readMessage(@RequestBody MessageReadForm request,
                         @ApiIgnore @LoginUser AppUserEntity user){
        messageService.readMessage(request, user.getUid());
        return new Result();
    }

    @Login
    @GetMapping("/readAllWatchInfo")
    @ApiOperation("设置所有用户关注消息为已读")
    public Result readAllWatchInfo(@ApiIgnore @LoginUser AppUserEntity user){
        messageService.readAllWatchInfo(user.getUid());
        return new Result();
    }

    @Login
    @PostMapping("/articleMsgState")
    @ApiOperation("设置系统消息为已读")
    public Result articleMsgState(@RequestBody UpdateSystemNoticeStatusForm request,
                             @ApiIgnore @LoginUser AppUserEntity user){
        Boolean status = messageService.articleMsgState(request, user.getUid());
        if(status){
            return new Result();
        }
        return new Result().error();
    }

    @Login
    @PostMapping("/updateSystemStatus")
    @ApiOperation("设置所有系统消息为已读")
    public Result updateSystemStatus(@RequestBody UpdateChatStatusForm request,@ApiIgnore  @LoginUser AppUserEntity user){
        messageService.updateSystemStatus(request, user.getUid());
        return new Result();
    }

    @Login
    @PostMapping("/delSystemMsg")
    @ApiOperation("用户删除系统消息")
    public Result delSystemMsg(@ApiIgnore  @LoginUser AppUserEntity user){
        messageService.delSystemMsg(user.getUid());
        return new Result();
    }
}
